package com.china08.yxyapi.service.manage.tec.teaching.homework.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkSingleQuesController.RespModel;
import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkSingleQuesController.SelectPeople;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.entity.mg.res.AllTemplate;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkInfoAnalyseSingleQuesService;

@Service
public class HomeworkInfoAnalyseSingleQuesServiceImpl implements HomeworkInfoAnalyseSingleQuesService {

    @Autowired
    ElasticQuesService elasticQuesService;

    @Override
    public List<RespModel> convertRespModel(List<HomeworkTaskQues> homeworkTaskQueslist,
            List<HomeworkTaskReportQues> homeworkTaskReportQueslist) {
        // return
        List<RespModel> respModels = new ArrayList<RespModel>();
        // ##
        // 获取试题Id
        List<String> quesIds = new ArrayList<>();
        homeworkTaskQueslist.forEach(homeworkTaskQues -> {
            quesIds.add(homeworkTaskQues.getQuesId());
        });
        List<ElasticYxyYxyQuesResource> questions = elasticQuesService.findByQuesIdIn(quesIds);
        Map<String, ElasticYxyYxyQuesResource> questionMap = new HashMap<String, ElasticYxyYxyQuesResource>();
        questions.forEach(question -> {
            questionMap.put(question.getId(), question);
        });
        // 组装数据
        homeworkTaskQueslist.forEach(homeworkTaskQues -> {
            RespModel respModel = new RespModel();
            respModel.order = homeworkTaskQues.getOrder();
            respModel.quesId = homeworkTaskQues.getQuesId();
            respModel.accuracy = 0d;
            respModel.quesId = homeworkTaskQues.getQuesId();
            respModel.quesBody = null;
            try {
                respModel.quesBody = questionMap.get(homeworkTaskQues.getQuesId());
            } catch (Exception e) {
            }
            respModel.answer = null;
            int optionlength = 0;
            try {
                optionlength = ((AllTemplate) questionMap.get(homeworkTaskQues.getQuesId()).getQuesBody()).sub.branch
                        .size();
            } catch (Exception e) {
            }
            List<SelectPeople> selectPeoples = new ArrayList<SelectPeople>();
            List<List<String>> students = new ArrayList<>();
            // 本题答对人数
            Integer exactnessTotal1 = 0;
            // 本题作答人数
            Integer total1 = 0;
            // ##
            for (int i = 1; i <= optionlength; i++) {
                // ##
                SelectPeople selectPeople = new SelectPeople();
                List<String> studentlist = new ArrayList<String>();
                int total = 0;
                // ##
                for (HomeworkTaskReportQues homeworkTaskReportQues : homeworkTaskReportQueslist) {
                    if (homeworkTaskReportQues.getQuesId().equals(homeworkTaskQues.getQuesId())) {
                        // 试题正确答案
                        if (respModel.answer == null) {
                            respModel.answer = homeworkTaskReportQues.getAnswerOfQues();
                        }
                        // ##
                        String answerOfMy = homeworkTaskReportQues.getAnswerOfMy();
                        if (answerOfMy != null) {
                            answerOfMy = answerOfMy.toUpperCase();
                            if (answerOfMy.equals(this.optionIndex(i))) {
                                total++;
                                studentlist.add(homeworkTaskReportQues.getStudentName());
                            }
                        }
                    }
                }
                // ##
                selectPeople.option = this.optionIndex(i);
                selectPeople.total = total;
                // ##
                selectPeoples.add(selectPeople);
                students.add(studentlist);
            }
            // 本题班级正确率统计
            for (HomeworkTaskReportQues homeworkTaskReportQues : homeworkTaskReportQueslist) {
                if (homeworkTaskReportQues.getQuesId().equals(homeworkTaskQues.getQuesId())) {
                    // ##
                    total1++;
                    if (homeworkTaskReportQues.getPassed() != null && homeworkTaskReportQues.getPassed() == 1) {
                        exactnessTotal1++;
                    }
                }
            }
            respModel.selectPeoples = selectPeoples;
            respModel.students = students;
            respModel.accuracy = getDouble(exactnessTotal1, total1);
            respModels.add(respModel);
        });

        if (respModels.size() == 0) { return new ArrayList<>(); }

        return respModels;
    }

    /** 选项处理 */
    public String optionIndex(int index) {
        if (index == 1) {
            return "A";
        } else if (index == 2) {
            return "B";
        } else if (index == 3) {
            return "C";
        } else if (index == 4) {
            return "D";
        } else if (index == 5) {
            return "E";
        } else if (index == 6) {
            return "F";
        } else if (index == 7) {
            return "G";
        } else if (index == 8) {
            return "H";
        } else if (index == 9) {
            return "I";
        } else {
            return "Z";
        }
    }

    // 格式转化
    private Double getDouble(Integer exactnessTotal1, Integer total1) {
        if (total1 == 0) { return 0d; }
        if (exactnessTotal1 <= 0) { return 0d; }
        Double x1 = (exactnessTotal1.doubleValue() / total1.doubleValue()) * 100d;
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        double parse = Double.parseDouble(df.format(x1));
        return parse;
    }
}
